{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4bbdb7a-2e34-4341-9799-3d3f6636c194",
   "metadata": {},
   "outputs": [],
   "source": [
    "from manim import *\n",
    "\n",
    "config.media_width = \"75%\"\n",
    "config.verbosity = \"WARNING\"\n",
    "config.tex_template = TexTemplateLibrary.ctex\n",
    "config.background_color = GREY\n",
    "\n",
    "HOME='../assets/Images'\n",
    "HOME2='../assets/SVG_Images'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b6420b37-17b7-4fdc-92e5-abecaa1c1557",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\AR@2024-12-09@13-52-43.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm AR\n",
    "class AR(Scene):\n",
    "    def construct(self):\n",
    "        r = ValueTracker(0.5)\n",
    "\n",
    "        circle = always_redraw(lambda: \n",
    "                Circle(radius=r.get_value(), stroke_color=YELLOW, \n",
    "                    stroke_width=5, color=RED))\n",
    "\n",
    "\n",
    "        line_radius = always_redraw(lambda: \n",
    "                Line(circle.get_center(), circle.get_bottom(), \n",
    "                    stroke_color = RED_B,  stroke_width = 10, color=BLUE))\n",
    "        \n",
    "        line_circumference = always_redraw(lambda: \n",
    "                Line(stroke_color = YELLOW,  stroke_width = 5, color=BLUE).set_length(2*r.get_value()*PI).next_to(circle, DOWN, buff=0.2))\n",
    "        \n",
    "        triangle = always_redraw(lambda :\n",
    "            Polygon(circle.get_top(),circle.get_left(),circle.get_right(), fill_color = GREEN_C))\n",
    "        \n",
    "        # 原作者的实现出现问题，需要修改\n",
    "        # self.play(LaggedStart(Create(circle), DrawBorderThenFill(line_radius),DrawBorderThenFill(triangle),\n",
    "        #                 run_time =4, lag_ratio =0.75))\n",
    "        # 目前不清楚为啥 Create 类型的动画和 always_redraw 的动画不能渲染同一个对象，估计是新版本api有所不同\n",
    "        # 这里使用copy复制出连个对象，一个实现创建动画，一个实现重绘动画。\n",
    "        # circle_c 做绘制动画，circle做always_redraw 重绘动画， 不能交换，应该是copy 没有复制到 ValueTracker 依赖。\n",
    "        circle_c = circle.copy() \n",
    "        line_radius_c = line_radius.copy()\n",
    "        triangle_c = triangle.copy()\n",
    "        self.play(LaggedStart(Create(circle_c), DrawBorderThenFill(line_radius_c),DrawBorderThenFill(triangle_c),\n",
    "                run_time =4, lag_ratio =0.75))\n",
    "        self.remove(circle_c, line_radius_c, triangle_c)\n",
    "        self.add(circle, line_radius, triangle)\n",
    "        \n",
    "        self.play(ReplacementTransform(circle.copy(),line_circumference),run_time = 2)\n",
    "        self.play(r.animate.set_value(2),run_time =5)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4aa08cb2-fcfa-4753-8ee0-0a1a04d18dde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\GM@2024-12-09@09-42-34.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm GM\n",
    "\n",
    "class GM(Scene):\n",
    "    def construct(self):\n",
    "        axes = Axes(x_range=[0,5,1], y_range=[0,3,1], x_length=5, y_length=3, \n",
    "                    axis_config = {\"include_tip\": True, \"numbers_to_exclude\": [0]}\n",
    "                )\n",
    "        axes.add_coordinates() \n",
    "        axes.to_edge(UR)\n",
    "        axis_labels = axes.get_axis_labels(x_label=\"x\", y_label=\"f(x)\")\n",
    "\n",
    "        graph = axes.plot(lambda x:x**0.5,x_range = [0,4], color = YELLOW)\n",
    "        graphing_stuff = VGroup(axes,graph,axis_labels)\n",
    "\n",
    "        self.play(DrawBorderThenFill(axes), Write(axis_labels))\n",
    "        self.play(Create(graph))\n",
    "        self.play(graphing_stuff.animate.shift(DOWN*4))\n",
    "        self.play(axes.animate.shift(LEFT*3), axis_labels.animate.shift(LEFT*3), run_time=3)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1b01cffb-9fd0-45ea-b097-1040731b0ede",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\GM2@2024-12-09@10-26-59.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm GM2\n",
    "class GM2(Scene):\n",
    "    def construct(self):\n",
    "        my_plane = NumberPlane(x_range=[-6,6], x_length = 5, y_range=[-10, 10], y_length = 5)\n",
    "        my_plane.add_coordinates()\n",
    "        my_plane.shift(RIGHT*3)\n",
    "\n",
    "        my_function = my_plane.plot(lambda x: 0.1*(x-5)*x*(x+5), x_range=[-6,6], color=GREEN_B)\n",
    "        area = my_plane.get_area(graph = my_function, x_range=[-5,5], color=[BLUE, YELLOW])\n",
    "\n",
    "        label = MathTex(\"f(x) = 0.1x(x-5)(x+5)\").next_to(my_plane, UP, buff=0.2)\n",
    "\n",
    "        horiz_line = Line(start = my_plane.c2p(0, my_function.underlying_function(-2)),\n",
    "                            end = my_plane.c2p(-2, my_function.underlying_function(-2)),\n",
    "                            stroke_color = YELLOW, stroke_width = 10)\n",
    "\n",
    "        self.play(DrawBorderThenFill(my_plane))\n",
    "        self.play(Create(my_function), Write(label))\n",
    "        self.play(FadeIn(area))\n",
    "        self.play(Create(horiz_line))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "40ea912a-555d-4dd4-aca2-a0d766cef734",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\CoordinateSystem@2024-12-07@10-34-42.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm CoordinateSystem\n",
    "class CoordinateSystem(Scene):\n",
    "    def construct(self):\n",
    "        plane = NumberPlane(x_range=[-4,4,1], x_length=4, y_range=[0,20,5], y_length=4).add_coordinates()\n",
    "        plane.shift(LEFT*3+DOWN*1)\n",
    "        plane_graph = plane.plot(lambda x:x**2, x_range=[-4,4], color=GREEN)\n",
    "        area = plane.get_riemann_rectangles(graph = plane_graph, x_range=[-2,2], dx=0.05)\n",
    "\n",
    "        axes = Axes(x_range=[-4,4,1],x_length=4, y_range=[-20,20,5], y_length=4).add_coordinates()\n",
    "        axes.shift(RIGHT*3+DOWN*1)\n",
    "        axes_graph = axes.plot(lambda x:2*x, x_range=[-4,4], color=YELLOW)\n",
    "        v_lines = axes.get_vertical_lines_to_graph(graph=axes_graph, x_range=[-3,3], num_lines=12)\n",
    "\n",
    "        self.play(Write(plane), Create(axes))\n",
    "        self.play(Create(plane_graph), Create(axes_graph), run_time=2)\n",
    "        self.add(area, v_lines)\n",
    "        self.wait(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8462781b-04fb-42b2-8527-639e8a4b8de5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Tute2@2024-12-07@12-47-47.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Tute2\n",
    "class Tute2(Scene):\n",
    "    def construct(self):\n",
    "        e = ValueTracker(0.01)\n",
    "\n",
    "        plane=PolarPlane(radius_max=3).add_coordinates()\n",
    "        plane.shift(LEFT*2)\n",
    "        graph1 = always_redraw(lambda: ParametricFunction(lambda t: plane.polar_to_point(2*np.sin(3*t),t), t_range=[0, e.get_value()], color=GREEN))\n",
    "        dot1 = always_redraw(lambda: Dot(fill_color = RED, fill_opacity=0.8).scale(.5).move_to(graph1.get_end()))\n",
    "\n",
    "        axes = Axes(x_range=[0,4,1], x_length=3, y_range=[-3,3,1], y_length=3).shift(RIGHT*4)\n",
    "        axes.add_coordinates()\n",
    "        graph2 = always_redraw(lambda: axes.plot(lambda x: 2*np.sin(3*x), x_range=[0, e.get_value()], color=GREEN))\n",
    "        dot2 = always_redraw(lambda: Dot(fill_color = RED, fill_opacity=0.8).scale(.5).move_to(graph2.get_end()))\n",
    "\n",
    "        title = MathTex(\"f(\\\\theta) = 2sin(3\\\\theta)\", color=GREEN).next_to(axes, UP, buff=.2)\n",
    "\n",
    "        self.play(LaggedStart( Write(plane), Create(axes), Write(title), run_time=3, lag_ratio=0.5))\n",
    "\n",
    "        self.add(graph1, graph2, dot1, dot2)\n",
    "        self.play(e.animate.set_value(PI), run_time=10, rate_func=linear)\n",
    "        self.wait(1)\n",
    "               \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b523635a-5c84-4bf0-9c10-589a6d0357f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Vectors@2024-12-07@12-43-20.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Vectors\n",
    "class Vectors(VectorScene):\n",
    "    def construct(self):\n",
    "        plane = self.add_plane(animate=True).add_coordinates()\n",
    "        vector = self.add_vector([-3,-2], color=YELLOW)\n",
    "\n",
    "        basis = self.get_basis_vectors()\n",
    "        self.add(basis)\n",
    "        self.vector_to_coords(vector=vector)\n",
    "\n",
    "        vector2 = self.add_vector([2,2])\n",
    "        self.write_vector_coordinates(vector=vector2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ae7b1d76-fd06-4cd1-907a-1789877be145",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "ename": "TypeError",
     "evalue": "Matrix.__init__() got an unexpected keyword argument 'renderer'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mget_ipython\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_cell_magic\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mmanim\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m-qm Matrix\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mimport random \u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43mclass Matrix(LinearTransformationScene):\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m    def __init__(self):\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        LinearTransformationScene.__init__(\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m            self, \u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m            show_coordinates=True, \u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m            leave_ghost_vectors=True, \u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m            show_basis_vectors=True\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        )\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m    def construct(self):\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        matrix = [[1,2],[2,1]]\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        matrix_tex = MathTex(\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mA = \u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43mbegin\u001b[39;49m\u001b[38;5;132;43;01m{bmatrix}\u001b[39;49;00m\u001b[38;5;124;43m 1 & 2 \u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m 2 & 1 \u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43mend\u001b[39;49m\u001b[38;5;132;43;01m{bmatrix}\u001b[39;49;00m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m).to_edge(UL).add_background_rectangle()\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        unit_square = self.get_unit_square()\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        text = always_redraw(lambda: Tex(\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mDet(A)\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m).set(width=0.7).move_to(unit_square.get_center()))\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        vect = self.get_vector([1, -2], color=PURPLE_B)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        rect1 = Rectangle(height=2, width=1, stroke_color=BLUE_A, fill_color=BLUE_D, fill_opacity = 0.6).shift(UP*2+LEFT*2)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        circ1 = Circle(radius=1, stroke_color=BLUE_A, fill_color=BLUE_D, fill_opacity = 0.6).shift(DOWN*2+RIGHT*1)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        self.add_transformable_mobject(vect, unit_square, rect1, circ1)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        self.add_background_mobject(matrix_tex, text)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        self.apply_matrix(matrix)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m        self.wait()\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m                      \u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\workspace\\code\\video\\vi-manim\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:2541\u001b[0m, in \u001b[0;36mInteractiveShell.run_cell_magic\u001b[1;34m(self, magic_name, line, cell)\u001b[0m\n\u001b[0;32m   2539\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbuiltin_trap:\n\u001b[0;32m   2540\u001b[0m     args \u001b[38;5;241m=\u001b[39m (magic_arg_s, cell)\n\u001b[1;32m-> 2541\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   2543\u001b[0m \u001b[38;5;66;03m# The code below prevents the output from being displayed\u001b[39;00m\n\u001b[0;32m   2544\u001b[0m \u001b[38;5;66;03m# when using magics with decorator @output_can_be_silenced\u001b[39;00m\n\u001b[0;32m   2545\u001b[0m \u001b[38;5;66;03m# when the last Python token in the expression is a ';'.\u001b[39;00m\n\u001b[0;32m   2546\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(fn, magic\u001b[38;5;241m.\u001b[39mMAGIC_OUTPUT_CAN_BE_SILENCED, \u001b[38;5;28;01mFalse\u001b[39;00m):\n",
      "File \u001b[1;32mD:\\workspace\\code\\video\\vi-manim\\Lib\\site-packages\\manim\\utils\\ipython_magic.py:141\u001b[0m, in \u001b[0;36mManimMagic.manim\u001b[1;34m(self, line, cell, local_ns)\u001b[0m\n\u001b[0;32m    139\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m    140\u001b[0m     SceneClass \u001b[38;5;241m=\u001b[39m local_ns[config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mscene_names\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;241m0\u001b[39m]]\n\u001b[1;32m--> 141\u001b[0m     scene \u001b[38;5;241m=\u001b[39m \u001b[43mSceneClass\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    142\u001b[0m     scene\u001b[38;5;241m.\u001b[39mrender()\n\u001b[0;32m    143\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m    144\u001b[0m     \u001b[38;5;66;03m# Shader cache becomes invalid as the context is destroyed\u001b[39;00m\n",
      "\u001b[1;31mTypeError\u001b[0m: Matrix.__init__() got an unexpected keyword argument 'renderer'"
     ]
    }
   ],
   "source": [
    "%%manim -qm Matrix  \n",
    "\n",
    "import random \n",
    "# 在jupyter中运行失败， 应该是 该版本的 LinearTransformationScene 在jupyter的参数不兼容\n",
    "class Matrix(LinearTransformationScene):\n",
    "    def __init__(self):\n",
    "        LinearTransformationScene.__init__(\n",
    "            self, \n",
    "            show_coordinates=True, \n",
    "            leave_ghost_vectors=True, \n",
    "            show_basis_vectors=True\n",
    "        )\n",
    "\n",
    "    def construct(self):\n",
    "        matrix = [[1,2],[2,1]]\n",
    "        matrix_tex = MathTex(\"A = \\\\begin{bmatrix} 1 & 2 \\\\ 2 & 1 \\\\end{bmatrix}\").to_edge(UL).add_background_rectangle()\n",
    "\n",
    "        unit_square = self.get_unit_square()\n",
    "        text = always_redraw(lambda: Tex(\"Det(A)\").set(width=0.7).move_to(unit_square.get_center()))\n",
    "\n",
    "        vect = self.get_vector([1, -2], color=PURPLE_B)\n",
    "\n",
    "        rect1 = Rectangle(height=2, width=1, stroke_color=BLUE_A, fill_color=BLUE_D, fill_opacity = 0.6).shift(UP*2+LEFT*2)\n",
    "        circ1 = Circle(radius=1, stroke_color=BLUE_A, fill_color=BLUE_D, fill_opacity = 0.6).shift(DOWN*2+RIGHT*1)\n",
    "\n",
    "        self.add_transformable_mobject(vect, unit_square, rect1, circ1)\n",
    "        self.add_background_mobject(matrix_tex, text)\n",
    "        self.apply_matrix(matrix)\n",
    "\n",
    "        self.wait()\n",
    "                      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "f4bc5e81-42f2-4e2e-bf41-f179981d77ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Tute1@2024-12-07@13-58-55.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Tute1  \n",
    "class Tute1(Scene):\n",
    "    def construct(self):\n",
    "        plane = NumberPlane(x_range=[-5,5,1], y_range=[-4,4,1], x_length=10, y_length=7)\n",
    "        plane.add_coordinates()\n",
    "        plane.shift(RIGHT*2)\n",
    "\n",
    "        vect1 = Line(start=plane.coords_to_point(0,0), end = plane.coords_to_point(3,2), stroke_color=YELLOW).add_tip()\n",
    "        vect1_name = MathTex(\"\\\\vec{v}\").next_to(vect1, RIGHT, buff=.1).set_color(YELLOW)\n",
    "        \n",
    "        vect2 = Line(start=plane.coords_to_point(0,0), end = plane.coords_to_point(-2,1), stroke_color=RED).add_tip()\n",
    "        vect2_name = MathTex(\"\\\\vec{w}\").next_to(vect2, LEFT, buff=.1).set_color(RED)\n",
    "\n",
    "        vect3 = Line(start=plane.coords_to_point(3,2), end = plane.coords_to_point(1,3), stroke_color=RED).add_tip()\n",
    "        \n",
    "        vect4 = Line(start=plane.coords_to_point(0,0), end = plane.coords_to_point(1,3), stroke_color=GREEN).add_tip()\n",
    "        vect4_name = MathTex(\"\\\\vec{v}+\\\\vec{w}\").next_to(vect4, LEFT, buff=.1).set_color(GREEN)\n",
    "\n",
    "        stuff = VGroup(plane, vect1, vect1_name, vect2, vect2_name, vect3,  vect4, vect4_name)\n",
    "\n",
    "        box = RoundedRectangle(height=1.5, width=1.5, corner_radius=.1, stroke_color=PINK).to_edge(DL)\n",
    "\n",
    "        self.play(DrawBorderThenFill(plane), run_time=2)\n",
    "        self.wait()\n",
    "        self.play(GrowFromPoint(vect1, point=vect1.get_start()), Write(vect1_name), run_time=2)\n",
    "        self.wait()\n",
    "        self.play(GrowFromPoint(vect2, point=vect2.get_start()), Write(vect2_name), run_time=2)\n",
    "        self.wait()\n",
    "        self.play(Transform(vect2, vect3), vect2_name.animate.next_to(vect3, UP, buff=.1), run_time=2)\n",
    "        self.wait()\n",
    "        self.play(LaggedStart(GrowFromPoint(vect4, point=vect4.get_start()), Write(vect4_name), run_time=3, lag_ratio=1))\n",
    "        self.wait()\n",
    "        self.add(box)\n",
    "        self.wait()\n",
    "        self.play(stuff.animate.move_to(box.get_center()).set(width=1.2),run_time=3)\n",
    "        self.wait()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "8ef3bcc0-0037-41c2-90ba-50c249d12199",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Tute1@2024-12-07@14-56-18.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Tute1 \n",
    "class Tute1(ThreeDScene):\n",
    "    def construct(self):\n",
    "        axes = ThreeDAxes(\n",
    "            x_range=[-6,6,1],\n",
    "            y_range=[-6,6,1],\n",
    "            z_range=[-6,6,1],\n",
    "            x_length=8,\n",
    "            y_length=6,\n",
    "            z_length=6,\n",
    "        )\n",
    "\n",
    "        graph = axes.plot(lambda x: x**2, x_range=[-2, 2, 1], color=YELLOW)\n",
    "        rects = axes.get_riemann_rectangles( graph=graph, x_range=[-2,2], dx=.1, stroke_color=WHITE)\n",
    "\n",
    "        graph2 = axes.plot_parametric_curve( \n",
    "                    lambda t: np.array([np.cos(t), np.sin(t), t]),\n",
    "                    t_range=[-2*PI, 2*PI],\n",
    "                    color = RED,\n",
    "                )\n",
    "\n",
    "        self.add(axes, graph)\n",
    "        self.wait()\n",
    "        \n",
    "        # 相机被自动设置为 phi=0， theta=90\n",
    "        \n",
    "        self.move_camera(phi=60*DEGREES) # 上下\n",
    "        self.wait()\n",
    "        self.move_camera(theta=45*DEGREES) # 左右\n",
    "        self.begin_ambient_camera_rotation(rate=PI/10, about=\"theta\") #每秒移动的角度\n",
    "        self.wait()\n",
    "        self.play(Create(rects), run_time=3)\n",
    "        self.play(Create(graph2))\n",
    "        self.wait()\n",
    "        self.stop_ambient_camera_rotation()\n",
    "        self.wait()\n",
    "        self.begin_ambient_camera_rotation(rate=PI/10, about=\"phi\") #每秒移动的角度\n",
    "        self.wait(2)\n",
    "        self.stop_ambient_camera_rotation()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "a83bcdd6-6221-4182-a24a-ae9ab626a1ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Tute1@2024-12-07@15-09-57.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Tute1 \n",
    "class Tute1(ThreeDScene):\n",
    "    def construct(self):\n",
    "        self.set_camera_orientation(phi=60*DEGREES, theta=-45*DEGREES)\n",
    "        axes = ThreeDAxes()\n",
    "        \n",
    "        graph = axes.plot(lambda x:x**2, x_range=[-2,2], color=YELLOW)\n",
    "        # surface = ParametricSurface(\n",
    "        #             lambda u,v: axes.c2p(v*np.cos(u), v*np.sin(u), .5*v**2),\n",
    "        #             u_min = 0,\n",
    "        #             u_max = 2*PI,\n",
    "        #             v_min = 0,\n",
    "        #             v_max = 3,\n",
    "        #             checkerboard_colors=[GREEN, RED],\n",
    "        #         )\n",
    "        surface = Surface(\n",
    "                    lambda u,v: axes.c2p(v*np.cos(u), v*np.sin(u), .5*v**2),\n",
    "                    u_range=[0, 2*PI],\n",
    "                    v_range=[0, 3],\n",
    "                    checkerboard_colors=[GREEN, RED],\n",
    "                )\n",
    "        three_d_stuff  = VGroup(axes, graph, surface)\n",
    "\n",
    "        self.add(axes, graph)\n",
    "        self.begin_ambient_camera_rotation(rate=PI/20) #每秒移动的角度\n",
    "        self.play(Create(surface))\n",
    "        self.play(three_d_stuff.animate.shift(LEFT*3))\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "7491e6b8-318e-44a1-820a-2d96176bb791",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Tute1@2024-12-07@15-37-43.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Tute1 \n",
    "class Tute1(ThreeDScene):\n",
    "    def construct(self):\n",
    "        self.set_camera_orientation(phi=45*DEGREES, theta=-45*DEGREES)\n",
    "        axes = ThreeDAxes(y_range=[-3, 10, 3], y_length=7).add_coordinates()\n",
    "\n",
    "        graph = axes.plot(lambda x: x, x_range=[0,3], color=RED_B)\n",
    "\n",
    "        area = axes.get_area(graph=graph, x_range=[0,3])\n",
    "\n",
    "        e = ValueTracker(0)\n",
    "        \n",
    "        surface = always_redraw(lambda: Surface(\n",
    "                        lambda u,v: axes.c2p(v, v*np.cos(u), v*np.sin(u)),\n",
    "                        u_range=[0, e.get_value()],\n",
    "                        v_range=[0, 3],\n",
    "                        checkerboard_colors=[GREEN, PURPLE],\n",
    "                    )\n",
    "                )\n",
    "        self.add(axes, surface)\n",
    "        self.begin_ambient_camera_rotation(rate=PI/15)\n",
    "        self.play(LaggedStart(Create(graph), Create(area)))\n",
    "        self.play(\n",
    "            Rotating(area, axis=RIGHT, radians=2*PI, about_point=axes.c2p(0,0,0)),\n",
    "            e.animate.set_value(2*PI),\n",
    "            run_time = 6,\n",
    "            rate_func=linear,\n",
    "        )\n",
    "        self.stop_ambient_camera_rotation()\n",
    "        self.wait()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "6989d663-1b00-47f1-b123-b830b326d675",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Derivatives@2024-12-07@16-20-58.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Derivatives \n",
    "\n",
    "def get_horizontal_line_to_graph(axes, function, x, width, color):\n",
    "    result=VGroup()\n",
    "    line = DashedLine(\n",
    "        start=axes.c2p(0, function.underlying_function(x)),\n",
    "        end=axes.c2p(x, function.underlying_function(x)),\n",
    "        stroke_width=width,\n",
    "        stroke_color=color,\n",
    "    )\n",
    "    dot = Dot().set_color(color).move_to(axes.c2p(x, function.underlying_function(x)))\n",
    "    result.add(line, dot)\n",
    "    return result\n",
    "\n",
    "\n",
    "class Derivatives(Scene):\n",
    "    def construct(self):\n",
    "        k = ValueTracker(-3)\n",
    "\n",
    "        plane1 = ( NumberPlane(x_range=[-3,4,1], x_length=5, y_range=[-8,9,2], y_length=5).add_coordinates().shift(LEFT*3.5) )\n",
    "        func1 = plane1.plot(lambda x: (1/3)*x**3, x_range=[-3,3], color=RED_C)\n",
    "        func1_lab=(\n",
    "            MathTex(\"f(x) = \\\\frac{1}{3} {x}^{3}\")\n",
    "            .set(width=2.5)\n",
    "            .next_to(plane1, UP, buff=.2)\n",
    "            .set_color(RED_C)\n",
    "        )\n",
    "\n",
    "        moving_slop = always_redraw(\n",
    "            lambda: plane1.get_secant_slope_group(\n",
    "                x = k.get_value(),\n",
    "                graph=func1,\n",
    "                dx=.05,\n",
    "                secant_line_length=4,\n",
    "                secant_line_color=YELLOW,\n",
    "            ))\n",
    "        dot = always_redraw(lambda: Dot().move_to(plane1.c2p(k.get_value(), func1.underlying_function(k.get_value()))))\n",
    "        # print('get_value:', [-3, k.get_value()])\n",
    "        plane2 = ( NumberPlane(x_range=[-3,4,1], x_length=5, y_range=[0,11,2], y_length=5).add_coordinates().shift(RIGHT*3.5) )\n",
    "        func2 = always_redraw(lambda: plane2.plot(lambda x: x**2, x_range=[-3.1, k.get_value()], color=GREEN))\n",
    "        func2_lab=(\n",
    "                    MathTex(\"f(x) = {x}^{2}\")\n",
    "                    .set(width=2.5)\n",
    "                    .next_to(plane2, UP, buff=.2)\n",
    "                    .set_color(GREEN)\n",
    "                )\n",
    "\n",
    "        moving_h_line = always_redraw(\n",
    "                            lambda:get_horizontal_line_to_graph(\n",
    "                                axes=plane2, function=func2, x=k.get_value(),width=4, color=YELLOW\n",
    "                            )\n",
    "                        )\n",
    "\n",
    "        slop_value_text = (\n",
    "            Tex(\"Slop value:\")\n",
    "            .next_to(plane1, DOWN, buff=.1)\n",
    "            .set_color(YELLOW)\n",
    "            .add_background_rectangle()\n",
    "        )                                                \n",
    "        slop_value = always_redraw(\n",
    "            lambda: DecimalNumber(num_decimal_places=1)\n",
    "            .set_value(func2.underlying_function(k.get_value()))\n",
    "            .next_to(slop_value_text, RIGHT, buff=.1)\n",
    "            .set_color(YELLOW)\n",
    "        ).add_background_rectangle()\n",
    "\n",
    "        self.play(\n",
    "            LaggedStart(\n",
    "                DrawBorderThenFill(plane1),\n",
    "                DrawBorderThenFill(plane2),\n",
    "                Create(func1),\n",
    "                Write(func1_lab),\n",
    "                Write(func2_lab),\n",
    "                run_time=5,\n",
    "                lag_ratio=.5,\n",
    "            )\n",
    "        )\n",
    "\n",
    "        self.add(moving_slop, moving_h_line, func2, slop_value, slop_value_text, dot)\n",
    "        self.play(k.animate.set_value(3), run_time=15, rate_func=linear)\n",
    "        self.wait()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "3ec6f03e-f639-4312-bc90-64fcefac104f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<string>:79: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.\n",
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\AdditiveFunctions@2024-12-07@17-27-35.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm AdditiveFunctions \n",
    "class AdditiveFunctions(Scene):\n",
    "    def construct(self):\n",
    "        axes = (\n",
    "            Axes(x_range=[0, 2.1, 1], x_length=12, y_range=[0,7,2], y_length=7)\n",
    "            .add_coordinates()\n",
    "            .to_edge(DL, buff=.25)\n",
    "            .scale(.8)\n",
    "        )\n",
    "        \n",
    "        func1 = axes.plot(lambda x: x**2, x_range=[0, 2], color=YELLOW)\n",
    "        func1_lab=(\n",
    "                    MathTex(\"f(x) = {x}^{2}\")\n",
    "                    # .set(width=2.5)\n",
    "                    .scale(.8)\n",
    "                    .next_to(func1, UR, buff=0)\n",
    "                    .set_color(YELLOW)\n",
    "                )\n",
    "        func2 = axes.plot(lambda x: x, x_range=[0, 2], color=GREEN)\n",
    "        func2_lab=(\n",
    "                    MathTex(\"f(x) = x\")\n",
    "                    .next_to(func2, UR, buff=0)\n",
    "                    .set_color(GREEN)\n",
    "                )\n",
    "        func3 = axes.plot(lambda x: x**2+x, x_range=[0, 2], color=PURPLE_D)\n",
    "        func3_lab=(\n",
    "                    MathTex(\"f(x) = {x}^{2}+x\")\n",
    "                    .scale(.8)\n",
    "                    .next_to(func3, UR, buff=0)\n",
    "                    .set_color(PURPLE_D)\n",
    "                )\n",
    "        self.add(axes, func1, func2, func3, func1_lab, func2_lab, func3_lab)\n",
    "        self.wait()\n",
    "\n",
    "        for k in np.arange(.2, 2.1, .2):\n",
    "            line1 = DashedLine(\n",
    "                start = axes.c2p(k,0),\n",
    "                end=axes.c2p(k, func1.underlying_function(k)),\n",
    "                stroke_color=YELLOW,\n",
    "                stroke_width=5,\n",
    "            )\n",
    "            \n",
    "            line2 = DashedLine(\n",
    "                start = axes.c2p(k,0),\n",
    "                end=axes.c2p(k, func2.underlying_function(k)),\n",
    "                stroke_color=GREEN,\n",
    "                stroke_width=7,\n",
    "            )\n",
    "            \n",
    "            line3 = Line(\n",
    "                start = axes.c2p(k,0),\n",
    "                end=axes.c2p(k, func3.underlying_function(k)),\n",
    "                stroke_color=PURPLE_D,\n",
    "                stroke_width=10,\n",
    "            )\n",
    "\n",
    "            self.play(Create(line1))\n",
    "            self.play(Create(line2))\n",
    "\n",
    "            if len(line1) >len(line2):\n",
    "                self.play(line2.animate.shift(UP*line1.get_length()))\n",
    "            else:\n",
    "                self.play(line1.animate.shift(UP*line2.get_length()))\n",
    "\n",
    "            self.play(Create(line3))\n",
    "        self.wait()\n",
    "\n",
    "        area1 = axes.get_riemann_rectangles(\n",
    "            graph=func1, x_range=[0,2], dx=.1, color=[BLUE, GREEN]\n",
    "        )\n",
    "        area2 = axes.get_riemann_rectangles(\n",
    "            graph=func2, x_range=[0,2], dx=.1, color=[YELLOW, PURPLE]\n",
    "        )\n",
    "\n",
    "        self.play(Create(area1))\n",
    "        self.play(area1.animate.set_opacity(.5))\n",
    "        self.play(Create(area2))\n",
    "        self.wait()\n",
    "        for k in range(20):\n",
    "            self.play(area2[k].animate.shift(UP*area1[k].get_height()))\n",
    "        self.wait()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a04f9341-f000-4e0f-9f20-bd0c3418bc21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\ArcLength@2024-12-09@10-53-59.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm ArcLength \n",
    "\n",
    "\n",
    "def get_arc_lines_on_function(\n",
    "    graph, plane, dx=1, line_color=WHITE, line_width=1, x_min=None, x_max=None\n",
    "):\n",
    "    dots=VGroup()\n",
    "    lines = VGroup()\n",
    "    result = VGroup(dots, lines)\n",
    "\n",
    "    x_range = np.arange(x_min, x_max, dx)\n",
    "    colors = color_gradient([BLUE_B, GREEN_B], len(x_range))\n",
    "\n",
    "    for x, color in zip(x_range, colors):\n",
    "        p1 = Dot().scale(.7).move_to(plane.input_to_graph_point(x, graph))\n",
    "        p2 = Dot().scale(.7).move_to(plane.input_to_graph_point(x+dx, graph))\n",
    "\n",
    "        dots.add(p1, p2)\n",
    "        dots.set_fill(colors, opacity=.8)\n",
    "\n",
    "        line = Line(\n",
    "            p1.get_center(),\n",
    "            p2.get_center(),\n",
    "            stroke_color = line_color,\n",
    "            stroke_width = line_width,\n",
    "        )\n",
    "        lines.add(line)\n",
    "    return result\n",
    "\n",
    "class ArcLength(Scene):\n",
    "    def construct(self):\n",
    "        axes = (\n",
    "            Axes(x_range=[-1, 4.1, 1], x_length=8, y_range=[0,3.1,1],y_length=6)\n",
    "            .to_edge(DL)\n",
    "            .add_coordinates()\n",
    "        )\n",
    "        labels = axes.get_axis_labels(x_label=\"x\", y_label=\"f(x)\")\n",
    "\n",
    "        graph = axes.plot(\n",
    "            lambda x: .1*x*(x+1)*(x-3)+1, x_range=[-1, 4], color=BLUE\n",
    "        )\n",
    "\n",
    "        dist = ValueTracker(1)\n",
    "\n",
    "        dx = always_redraw(\n",
    "                lambda: DashedLine(\n",
    "                    start = axes.c2p(2, graph.underlying_function(2)),\n",
    "                    end = axes.c2p(2 + dist.get_value(), graph.underlying_function(2)),\n",
    "                    stroke_color=GREEN,\n",
    "                ))\n",
    "        dx_brace = always_redraw(lambda: Brace(dx).next_to(dx, DOWN, buff=.1))\n",
    "        dx_text = always_redraw(\n",
    "            lambda: MathTex('dx').set(width=.3).next_to(dx_brace, DOWN, buff=0)\n",
    "        )\n",
    "        dy = always_redraw(\n",
    "                lambda: DashedLine(\n",
    "                    start = axes.c2p(2+ dist.get_value(), graph.underlying_function(2+ dist.get_value())),\n",
    "                    end = axes.c2p(2 + dist.get_value(), graph.underlying_function(2)),\n",
    "                    stroke_color=GREEN,\n",
    "                ))\n",
    "        dy_brace = always_redraw(lambda: Brace(dy, direction=RIGHT).next_to(dy, RIGHT, buff=.1))\n",
    "        dy_text = always_redraw(\n",
    "            lambda: MathTex('dy').set(width=.3).next_to(dy_brace, RIGHT, buff=0)\n",
    "        )\n",
    "\n",
    "        dl = always_redraw(\n",
    "            lambda: Line(\n",
    "                start = axes.c2p(2, graph.underlying_function(2)),\n",
    "                end = axes.c2p(2 + dist.get_value(), graph.underlying_function(2+dist.get_value())),\n",
    "                stroke_color=YELLOW,\n",
    "            )\n",
    "        )\n",
    "\n",
    "        dl_brace = always_redraw(lambda: BraceBetweenPoints(point_1 = dl.get_end(), point_2=dl.get_start()))\n",
    "        dl_text = always_redraw(\n",
    "            lambda: MathTex('dL').set(width=.3).next_to(dl_brace, UP, buff=0).set_color(YELLOW)\n",
    "        )\n",
    "\n",
    "        demo_mobjects = VGroup(dx, dx_brace, dx_text, dy, dy_brace, dy_text, dl, dl_brace, dl_text)\n",
    "\n",
    "        helper_text = (\n",
    "            # MathTex(\"dl \\\\ approximates \\\\ curve \\\\ as \\\\ dx \\\\ approaches \\\\ 0\")\n",
    "            Tex(\"当x的增量dx接近0的时候，那么dl将近似曲线\", tex_template=TexTemplateLibrary.ctex)\n",
    "            .set(width=6)\n",
    "            .to_edge(UR, buff=.2)\n",
    "        )\n",
    "        line1 = MathTex(\"{dL}^{2}={dx}^{2}+{dy}^{2}\")\n",
    "        line2 = MathTex(\"{dL}^{2}={dx}^{2}(1+(\\\\frac{dy}{dx})^{2})\")\n",
    "        line3 = MathTex(\"dL = \\\\sqrt{ {dx}^{2}(1+(\\\\frac{dy}{dx})^{2}) }\")\n",
    "        line4 = MathTex(\"dL = \\\\sqrt{1 + (\\\\frac{dy}{dx})^{2} } dx\")\n",
    "        proof = (\n",
    "            VGroup(line1, line2, line3, line4)\n",
    "            .scale(.8)\n",
    "            .arrange(DOWN, aligned_edge=LEFT)\n",
    "            .next_to(helper_text, DOWN, buff=.25)\n",
    "        )\n",
    "\n",
    "        box = SurroundingRectangle(helper_text)\n",
    "\n",
    "        dx_tracker = ValueTracker(1)\n",
    "\n",
    "        line_integral = always_redraw(\n",
    "            lambda:get_arc_lines_on_function(\n",
    "                graph=graph,\n",
    "                plane=axes,\n",
    "                dx=dx_tracker.get_value(),\n",
    "                x_min=-1,\n",
    "                x_max=4,\n",
    "                line_color=RED,\n",
    "                line_width=5,\n",
    "            )\n",
    "        )\n",
    "\n",
    "        self.add(axes, graph)\n",
    "        self.wait()\n",
    "        self.play(Create(dx),Create(dy))\n",
    "        self.play(Create(dl))\n",
    "        self.add(dx_brace, dx_text, dy_brace, dy_text, dl_brace, dl_text)\n",
    "\n",
    "        self.play(Write(proof), run_time=5, rate_func=linear)\n",
    "        self.wait()\n",
    "        self.play(Write(helper_text))\n",
    "        self.play(Create(box), run_time=2)\n",
    "        self.play(dist.animate.set_value(.5), run_time=10)\n",
    "        self.play(\n",
    "            FadeOut(proof),\n",
    "            demo_mobjects.animate.set_width(.5).next_to(box, LEFT, buff=.1),\n",
    "            FadeOut(demo_mobjects),\n",
    "            run_time=3,\n",
    "        )\n",
    "\n",
    "        self.play(Create(line_integral))\n",
    "        self.play(dx_tracker.animate.set_value(.2),run_time=10)\n",
    "        self.wait()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cd5b436-ca6f-493e-876a-964a1725509f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\RandomNumbers@2024-12-08@09-17-20.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm RandomNumbers \n",
    "import random \n",
    "\n",
    "\n",
    "class RandomNumbers(Scene):\n",
    "    def construct(self):\n",
    "        numbers = VGroup()\n",
    "        for x in range(28):\n",
    "            num = DecimalNumber()\n",
    "            numbers.add(num)\n",
    "\n",
    "        def randomize_numbers(numbers):\n",
    "            for num in numbers:\n",
    "                value = random.uniform(0,1)\n",
    "                num.set_value(value)\n",
    "                num.set_color(GREEN if value>.1 else RED)\n",
    "\n",
    "\n",
    "        randomize_numbers(numbers)\n",
    "        numbers.set(width=.38)\n",
    "        numbers.arrange(RIGHT, buff=.1)\n",
    "        numbers.to_edge(UR)\n",
    "\n",
    "        def get_results(numbers):\n",
    "            results = VGroup()\n",
    "            for num in numbers:\n",
    "                if num.get_value() > .1:\n",
    "                    result = (\n",
    "                        SVGMobject(f'{HOME2}/green_tick.svg')\n",
    "                        .set_color(GREEN_C)\n",
    "                        .set(width=.3)\n",
    "                    )\n",
    "                else:\n",
    "                    result = (\n",
    "                        SVGMobject(f'{HOME2}/cross.svg')\n",
    "                        .set_color(RED_C)\n",
    "                        .set(width=.3)\n",
    "                    )\n",
    "                result.next_to(num, DOWN, buff=.2)\n",
    "                results.add(result)\n",
    "            return results\n",
    "        for k in range(10):\n",
    "            self.play(UpdateFromFunc(numbers, randomize_numbers))\n",
    "            self.wait()\n",
    "            result = get_results(numbers)\n",
    "            self.play(Write(result))\n",
    "            self.wait()\n",
    "            box = SurroundingRectangle(result)\n",
    "            self.play(Create(box))\n",
    "            self.play(FadeOut(box), FadeOut(result))\n",
    "        self.wait()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79f94d02-08a3-488e-9981-d026471bd548",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<string>:185: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.\n",
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\CentralLimitTheorem@2024-12-08@10-45-22.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm CentralLimitTheorem \n",
    "\n",
    "import random\n",
    "\n",
    "def get_data_1(self):\n",
    "    w = .2\n",
    "    t_row1 = (\n",
    "        VGroup(\n",
    "            *[\n",
    "                SVGMobject(f'{HOME2}/green_tick.svg').set(width=w).set_color(GREEN)\n",
    "                for i in range(10)\n",
    "            ]\n",
    "        )\n",
    "        .arrange(RIGHT, buff=.2)\n",
    "        .to_edge(UL, buff=.25)\n",
    "    )\n",
    "    \n",
    "    t_row2 = (\n",
    "        VGroup(\n",
    "            *[\n",
    "                SVGMobject(f'{HOME2}/green_tick.svg').set(width=w).set_color(GREEN)\n",
    "                for i in range(10)\n",
    "            ]\n",
    "        )\n",
    "        .arrange(RIGHT, buff=.2)\n",
    "        .next_to(t_row1, DOWN, buff=.25)\n",
    "    )\n",
    "    f_row1 = (\n",
    "        VGroup(\n",
    "            *[\n",
    "                SVGMobject(f'{HOME2}/cross.svg').set(width=w).set_color(RED)\n",
    "                for i in range(10)\n",
    "            ]\n",
    "        )\n",
    "        .arrange(RIGHT, buff=.2)\n",
    "        .next_to(t_row2, DOWN, buff=.25)\n",
    "    )\n",
    "    \n",
    "    f_row2 = (\n",
    "        VGroup(\n",
    "            *[\n",
    "                SVGMobject(f'{HOME2}/cross.svg').set(width=w).set_color(RED)\n",
    "                for i in range(10)\n",
    "            ]\n",
    "        )\n",
    "        .arrange(RIGHT, buff=.2)\n",
    "        .next_to(f_row1, DOWN, buff=.25)\n",
    "    )\n",
    "    f_row3 = (\n",
    "        VGroup(\n",
    "            *[\n",
    "                SVGMobject(f'{HOME2}/cross.svg').set(width=w).set_color(RED)\n",
    "                for i in range(10)\n",
    "            ]\n",
    "        )\n",
    "        .arrange(RIGHT, buff=.2)\n",
    "        .next_to(f_row2, DOWN, buff=.25)\n",
    "    )\n",
    "    result = VGroup(*t_row1, *t_row2, *f_row1, *f_row2, *f_row3)\n",
    "    return result\n",
    "\n",
    "class CentralLimitTheorem(Scene):\n",
    "    def construct(self):\n",
    "        data = get_data_1(self)\n",
    "        axes = (\n",
    "            Axes(x_range=[0,1.2,.1], y_range=[0,2.5], x_length=10, y_length=4)\n",
    "            .to_edge(DL)\n",
    "            .shift(UP*.2)\n",
    "        )\n",
    "\n",
    "        labels = axes.get_axis_labels(x_label='\\\\hat{p}', y_label='')\n",
    "\n",
    "        x_axis_nums = VGroup()\n",
    "        for i in range(11):\n",
    "            num = (\n",
    "                MathTex('\\\\frac{%3d}{10}'%i)\n",
    "                .scale(.6)\n",
    "                .next_to(axes.x_axis.n2p(i/10+.05), DOWN, buff=.1)\n",
    "            )\n",
    "            x_axis_nums.add(num)\n",
    "\n",
    "        sample_counter = Tex('Total samples: ').scale(.6).to_edge(UR).shift(LEFT*.6)\n",
    "        total_counter = (\n",
    "            Tex('Sum of Averages:')\n",
    "            .scale(.6)\n",
    "            .next_to(sample_counter, DOWN, aligned_edge=LEFT, buff=.4)\n",
    "        )\n",
    "        average_counter = (\n",
    "            MathTex('Average \\\\ \\\\hat{p}: ')\n",
    "            .scale(.6)\n",
    "            .next_to(total_counter, DOWN, aligned_edge=LEFT, buff=.4)\n",
    "        )\n",
    "\n",
    "        self.play(\n",
    "            LaggedStart(\n",
    "                Create(data),\n",
    "                Write(VGroup(sample_counter, total_counter, average_counter)),\n",
    "                Create(axes),\n",
    "                Write(x_axis_nums),\n",
    "                run_time=4,\n",
    "                lag_ratio =1,\n",
    "            )\n",
    "        )\n",
    "        self.wait()\n",
    "\n",
    "        data = get_data_1(self)\n",
    "        sample_count = 10\n",
    "        possible_outcomes = sample_count +1 \n",
    "\n",
    "        counter_num = 0\n",
    "        counter_number = (\n",
    "            Integer(counter_num).scale(.5).next_to(sample_counter, RIGHT, buff=.2)\n",
    "        )\n",
    "        counter_number.add_updater(lambda m: m.set_value(counter_num))\n",
    "\n",
    "        \n",
    "        total_sum = 0\n",
    "        total_number = (\n",
    "            DecimalNumber(total_sum).scale(.5).next_to(total_counter, RIGHT, buff=.2)\n",
    "        )\n",
    "        total_number.add_updater(lambda m: m.set_value(total_sum))\n",
    "\n",
    "        average = 0\n",
    "        average_num = (\n",
    "            DecimalNumber(average).scale(.5).next_to(average_counter, RIGHT, buff=.2)\n",
    "        )\n",
    "        average_num.add_updater(lambda m: m.set_value(average))\n",
    "\n",
    "        self.add(counter_number, total_number, average_num)\n",
    "\n",
    "        sums = [0] * possible_outcomes \n",
    "\n",
    "        for s in range(15):\n",
    "            a = random.sample(range(0, 50), k=sample_count)\n",
    "\n",
    "            sample_results = VGroup()\n",
    "            boxes = VGroup()\n",
    "            for i, res in enumerate(a):\n",
    "                res = data[a[i]]\n",
    "                box = SurroundingRectangle(res) \n",
    "                sample_results.add(res)\n",
    "                boxes.add(box) \n",
    "\n",
    "            moved_result = sample_results.copy()\n",
    "\n",
    "            self.play(Create(boxes))\n",
    "            self.play(\n",
    "                moved_result.animate.arrange(RIGHT*.3, buff=0).to_edge(UP),\n",
    "                FadeOut(boxes),\n",
    "            )\n",
    "\n",
    "            for i, value in enumerate(a):\n",
    "                a[i] = 1 if value < 20 else 0\n",
    "            \n",
    "            prop = DecimalNumber(num_decimal_places=1)\n",
    "            tot = sum(a)\n",
    "            prop.set_value(tot/sample_count).set(height=.2)\n",
    "            prop.next_to(moved_result, RIGHT, buff=.3)\n",
    "\n",
    "            axes_box = SurroundingRectangle(\n",
    "                moved_result,\n",
    "                stroke_color = WHITE,\n",
    "                stroke_width = .2,\n",
    "                fill_color = BLUE,\n",
    "                fill_opacity = .8,\n",
    "                buff = .1\n",
    "            )\n",
    "            stack_in_axes = VGroup(axes_box, moved_result)\n",
    "\n",
    "            self.play(DrawBorderThenFill(axes_box))\n",
    "            self.play(Write(prop))\n",
    "\n",
    "            counter_num += 1\n",
    "            total_sum += tot/sample_count\n",
    "            average = total_sum/counter_num\n",
    "\n",
    "            self.play(\n",
    "                stack_in_axes.animate.next_to(x_axis_nums[tot], UP, buff=0)\n",
    "                .set(width=.77)\n",
    "                .shift(UP*sums[tot]),\n",
    "                FadeOut(prop),\n",
    "            )\n",
    "            sums[tot] += stack_in_axes.get_height()\n",
    "\n",
    "        self.wait()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ea7e6444-fa83-4024-9a1c-9cc1865f4641",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\One@2024-12-09@10-58-12.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm One \n",
    "\n",
    "class One(ThreeDScene):\n",
    "    def construct(self):\n",
    "        l,w,h = 2, 4, 1\n",
    "\n",
    "        rect_prism = Prism(dimensions=[l,w,h]).to_edge(LEFT, buff=1)\n",
    "\n",
    "        kwargs = {\"stroke_color\": BLUE_D, \"fill_color\":BLUE_B, \"fill_opacity\":.8}\n",
    "        bottom = Rectangle(width=w, height=l, **kwargs)\n",
    "        s1 = Rectangle(height=h, width=w, **kwargs).next_to(bottom, UP, buff=0)\n",
    "        s2 = Rectangle(height=h, width=w, **kwargs).next_to(bottom, DOWN, buff=0)\n",
    "        l1 = Rectangle(height=l, width=h, **kwargs).next_to(bottom, LEFT, buff=0)\n",
    "        l2 = Rectangle(height=l, width=h, **kwargs).next_to(bottom, RIGHT, buff=0)\n",
    "        top = Rectangle(height=l, width=w, **kwargs).next_to(s1, UP, buff=0)\n",
    "        net = VGroup(top, bottom, s1, s2, l1, l2).rotate(-PI/2).to_edge(RIGHT, buff=1)\n",
    "\n",
    "        arrow = Line(\n",
    "                    start=rect_prism.get_right(), end=net.get_left(), buff=.2\n",
    "            ).add_tip()\n",
    "\n",
    "        self.begin_ambient_camera_rotation()\n",
    "        self.set_camera_orientation(phi=45*DEGREES, theta=45*DEGREES)\n",
    "        self.play(Create(rect_prism))\n",
    "        self.play(\n",
    "            LaggedStart(Create(arrow), Transform(rect_prism.copy(), net)),\n",
    "            run_time = 2,\n",
    "            lag_ratio=.5,\n",
    "        )\n",
    "        self.wait()\n",
    "        self.play(FadeOut(Group(*self.mobjects)))\n",
    "        self.stop_ambient_camera_rotation()\n",
    "\n",
    "        self.set_camera_orientation(phi=0, theta=-90*DEGREES)\n",
    "        # text = Tex('Surface Area of a Solid Revolutions?')\n",
    "        text = Tex('下面展示生成旋转体3D表面的过程', tex_template=TexTemplateLibrary.ctex)\n",
    "        self.play(Write(text))\n",
    "        self.wait()\n",
    "        self.play(FadeOut(text))\n",
    "\n",
    "        self.begin_ambient_camera_rotation()\n",
    "        self.set_camera_orientation(phi=45*DEGREES, theta=-45*DEGREES)\n",
    "\n",
    "        axes= ThreeDAxes(\n",
    "            x_range=[0, 4.1, 1],\n",
    "            x_length=5,\n",
    "            \n",
    "            y_range=[-4, 4.1, 1],\n",
    "            y_length=5,\n",
    "            \n",
    "            z_range=[-4, 4.1, 1],\n",
    "            z_length=5,\n",
    "            \n",
    "        ).add_coordinates()\n",
    "\n",
    "        function = axes.plot(lambda x: .25*x**2, x_range=[0,4], color=YELLOW)\n",
    "        area=axes.get_area(graph=function, x_range=[0,4], color=[BLUE_B, BLUE_D])\n",
    "\n",
    "        surface =  Surface(\n",
    "                        lambda u,v: axes.c2p(v, .25*v**2*np.cos(u), .25*v**2*np.sin(u)),\n",
    "                        u_range=[0, 2*PI],\n",
    "                        v_range=[0, 4],\n",
    "                        checkerboard_colors=[BLUE_B, BLUE_D],\n",
    "                    )\n",
    "        self.play(\n",
    "            LaggedStart(Create(axes), Create(function), Create(area), Create(surface)),\n",
    "            run_time=4,\n",
    "            lag_ratio=.5,\n",
    "        )\n",
    "\n",
    "        self.play(\n",
    "            Rotating(\n",
    "                VGroup(function, area),\n",
    "                axis=RIGHT,\n",
    "                radinas=2*PI,\n",
    "                about_point=axes.c2p(0,0,0),\n",
    "            ),\n",
    "            run_time=5,\n",
    "            rate_func = linear,\n",
    "        )\n",
    "        self.wait(3)\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "3363396b-afa4-4534-ba99-d27bdee4d4d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\ThreePers2@2024-12-08@17-08-28.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm ThreePers2 \n",
    "# 7 表面旋转\n",
    "def get_conic_approximations(axes,graph,x_min=0,x_max=1, dx=.5, color_A=RED, color_B=GREEN, opacity=1):\n",
    "    result = VGroup()\n",
    "    for x in np.arange(x_min+dx, x_max+dx, dx):\n",
    "        if graph.underlying_function(x) > 0:\n",
    "            k = 0\n",
    "            conic_surface = Surface(\n",
    "                lambda u,v: axes.c2p(u,k*v*np.cos(u), k*v*np.sin(u)),\n",
    "                u_range=[0, 2*PI],\n",
    "                v_range=[x - dx, x],\n",
    "                checkerboard_colors=[color_A, color_B],\n",
    "                fill_opacity=opacity,\n",
    "            )\n",
    "        result.add(conic_surface)\n",
    "    return result \n",
    "\n",
    "def get_riemann_truncated_cones(\n",
    "        axes, graph, x_min=0, x_max=1, dx=.5, color_A=RED, color_B=GREEN, opacity=1,stroke_color=WHITE, stroke_width=1,theta=45\n",
    "):\n",
    "    result = VGroup()\n",
    "    for x in np.arange(x_min, x_max, dx):\n",
    "        points = VGroup()\n",
    "        p1 = axes.c2p(x+dx,0)\n",
    "        p2 = axes.c2p(x+dx,graph.underlying_function(x+dx))\n",
    "        p3 = axes.c2p(x,graph.underlying_function(x))\n",
    "        p4 = axes.c2p(x,0)\n",
    "        truncated_conic = ArcPolygon(\n",
    "            p1, p2, p3, p4,\n",
    "            stroke_color=stroke_color,\n",
    "            stroke_width=stroke_width,\n",
    "            fill_color=[color_A, color_B],\n",
    "            fill_opacity=opacity,\n",
    "            arc_config=[\n",
    "                {'angle': theta*DEGREES, 'color': stroke_color},\n",
    "                {'angle': 0, 'color': stroke_color},\n",
    "                {'angle': -theta*DEGREES, 'color': stroke_color},\n",
    "                {'angle': 0, 'color': stroke_color},\n",
    "            ],\n",
    "        )\n",
    "        \n",
    "        result.add(truncated_conic)\n",
    "    return result\n",
    "\n",
    "class ThreePers2(ThreeDScene):\n",
    "    def construct(self):\n",
    "        axes = ThreeDAxes(\n",
    "            x_range=[0, 4.1, 1],\n",
    "            x_length=5,\n",
    "            y_range=[-4, 4.1, 1],\n",
    "            y_length=5,\n",
    "            z_range=[-4, 4, 1],\n",
    "            z_length=5,\n",
    "            axis_config={'decimal_number_config': {'num_decimal_places': 0}},\n",
    "        ).to_edge(LEFT)\n",
    "        axes.add_coordinates()\n",
    "\n",
    "        graph = axes.plot(lambda x: .25*x**0.5, x_range=[0, 4], color=YELLOW)\n",
    "        \n",
    "        surface = always_redraw(lambda: Surface(\n",
    "            lambda u, v: axes.c2p(v, .25*v**2*np.cos(u), .25*v**2*np.sin(u)),\n",
    "            u_range=[0, 2*PI],\n",
    "            v_range=[0, 4],\n",
    "            checkerboard_colors=[BLUE_B, BLUE_D]\n",
    "        ))\n",
    "\n",
    "        dx = ValueTracker(1)\n",
    "        conic_approx = always_redraw(\n",
    "            lambda: get_conic_approximations(axes=axes, graph=graph,x_min=0, x_max=4, dx=dx.get_value())\n",
    "        )\n",
    "\n",
    "        num_text = MathTex('dx=').next_to(axes, UP, buff=.5)\n",
    "        num = always_redraw(\n",
    "            lambda: DecimalNumber()\n",
    "            .set_value(dx.get_value())\n",
    "            .next_to(num_text, RIGHT, buff=.1)\n",
    "        )\n",
    "\n",
    "        axes2 = Axes(\n",
    "            x_range=[0, 4, 1],x_length=5, y_range=[0, 60, 10], y_length=6\n",
    "        ).to_edge(DR)\n",
    "\n",
    "        def sa_func(x):\n",
    "            return 6.2832*x*(1+(x**2/4))**.5\n",
    "        \n",
    "        graph2 = axes2.plot(sa_func, x_range=[0, 4], color=BLUE)\n",
    "        graph2_lab = Tex('SA Function').next_to(axes2, UP, buff=.2)\n",
    "\n",
    "        t = ValueTracker(45)\n",
    "        truncated_area = always_redraw(\n",
    "            lambda: get_riemann_truncated_cones(\n",
    "                axes=axes2,\n",
    "                graph=graph2,\n",
    "                x_min=0,\n",
    "                x_max=4,\n",
    "                dx=dx.get_value(),\n",
    "                theta=t.get_value(),\n",
    "            )\n",
    "        )\n",
    "\n",
    "        self.set_camera_orientation(phi=0*DEGREES, theta=-90*DEGREES)\n",
    "        self.add(axes, graph, surface, conic_approx, num_text, num)\n",
    "        self.move_camera(phi=30*DEGREES, theta=-100*DEGREES)\n",
    "        self.begin_ambient_camera_rotation(rate=0.01)\n",
    "        self.play(\n",
    "            LaggedStart(\n",
    "                Create(conic_approx),\n",
    "                Write(VGroup(num_text, num)),\n",
    "                DrawBorderThenFill(axes2),\n",
    "                run_time=1,\n",
    "                lag_ratio=.25,\n",
    "            )\n",
    "        )\n",
    "        self.play(ReplacementTransform(conic_approx.copy(), truncated_area), run_time=1)\n",
    "        self.play(dx.animate.set_value(.1), t.animate.set_value(5), run_time=3)\n",
    "        self.add(graph2, graph2_lab)\n",
    "        self.wait()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "92f27aed-41b8-4733-b1c2-1356388b70d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\Pendulum@2024-12-08@16-25-04.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm Pendulum \n",
    "# 钟摆 \n",
    "class Pendulum(Scene):\n",
    "    def construct(self):\n",
    "        time = ValueTracker(0)\n",
    "        theta_max = 30/180*PI \n",
    "        l, g = 3, 15 \n",
    "        w = np.sqrt(g/l)\n",
    "        T = 2*PI/w\n",
    "\n",
    "        p_x, p_y = -2.5, 3\n",
    "        shift_req = p_x * RIGHT + p_y * UP\n",
    "\n",
    "        theta = DecimalNumber().set_color(BLACK).move_to(10*RIGHT)\n",
    "        theta.add_updater(lambda m: m.set_value((theta_max)*np.sin(w*time.get_value())))\n",
    "\n",
    "        self.add(theta)\n",
    "\n",
    "        def get_line(x, y):\n",
    "            line_here = Line(start=ORIGIN+shift_req, end=x*RIGHT+y*UP+shift_req, color='#DDC5B8')\n",
    "            global line_vertical\n",
    "            line_vertical = DashedLine(start=line_here.get_start(), end=line_here.get_start()+3*DOWN, color='#DDC5B8')\n",
    "            return line_here\n",
    "\n",
    "        line = always_redraw(lambda: get_line(l*np.sin(theta.get_value()), -l*np.cos(theta.get_value())))\n",
    "\n",
    "        self.add(line)\n",
    "        self.add(line_vertical)\n",
    "        \n",
    "        def angle_arc(theta):\n",
    "            global angle \n",
    "            global arc_text \n",
    "            if theta == 0:\n",
    "                angle = VectorizedPoint().move_to(10*RIGHT)\n",
    "                arc_text = VectorizedPoint().move_to(10*RIGHT)\n",
    "            else:\n",
    "                if theta > 0:\n",
    "                    angle = Angle(line, line_vertical, quadrant=(1,1), other_angle=True, color=YELLOW, fill_opacity=0)\n",
    "                elif theta < 0:\n",
    "                    angle = Angle(line, line_vertical, quadrant=(1,1), other_angle=False, color=YELLOW, fill_opacity=0)\n",
    "            return angle \n",
    "        \n",
    "        angle = always_redraw(lambda: angle_arc(theta.get_value()))\n",
    "        self.add(angle)\n",
    "        arc_text = Tex(r'$\\theta$').scale(.5)\n",
    "        arc_text.add_updater(lambda m: m.next_to(angle, DOWN))\n",
    "        self.add(arc_text)\n",
    "\n",
    "        def get_ball(x, y):\n",
    "            dot = Dot(fill_color=BLUE_E, fill_opacity=1).move_to(x*RIGHT+y*UP+shift_req).scale(l)\n",
    "            return dot\n",
    "        \n",
    "        ball = always_redraw(lambda: get_ball(l*np.sin(theta.get_value()), -l*np.cos(theta.get_value())))\n",
    "        self.add(ball)\n",
    "\n",
    "        self.play(time.animate.set_value(3*T), rate_func=linear, run_time=3*T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "36ca1cc2-ad73-4b3f-b398-6f46a3cd694c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\CalculusSlopes@2024-12-09@11-31-33.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm CalculusSlopes \n",
    "# 微积分\n",
    "class CalculusSlopes(Scene):\n",
    "    def construct(self):\n",
    "        plane = NumberPlane(\n",
    "            x_range=[-3,3],\n",
    "            y_range=[-4, 14],\n",
    "            y_length=7, \n",
    "            x_length=6\n",
    "        ).add_coordinates()\n",
    "\n",
    "        graph1 = plane.plot(lambda x: x**2, x_range=[-3,3], color=RED)\n",
    "        graph1_lab = (\n",
    "            MathTex(\"f(x) = x^2\")\n",
    "            .next_to(graph1, UR, buff=.2)\n",
    "            .set_color(RED)\n",
    "            .scale(.8)\n",
    "        )\n",
    "\n",
    "        c = ValueTracker(-4)\n",
    "        graph2 = always_redraw(\n",
    "            lambda: plane.plot(\n",
    "                lambda x: x**2+c.get_value(), x_range=[-3,3], color=YELLOW\n",
    "            )\n",
    "        )\n",
    "        graph2_lab = always_redraw(\n",
    "            lambda: MathTex(\"f(x) = x^2+c\")\n",
    "            .next_to(graph2, UR, buff=.2)\n",
    "            .set_color(YELLOW)\n",
    "            .scale(.8)\n",
    "        )\n",
    "\n",
    "        k = ValueTracker(-3)\n",
    "        slope1 = always_redraw(\n",
    "            lambda: plane.get_secant_slope_group(x = k.get_value(), graph = graph1, dx = 0.01, secant_line_length=5)\n",
    "        )\n",
    "        dot1 = always_redraw(\n",
    "            lambda: Dot().move_to(plane.coords_to_point(\n",
    "                k.get_value(), graph1.underlying_function(k.get_value())\n",
    "            ))\n",
    "        )\n",
    "        slope2 = always_redraw(\n",
    "            lambda: plane.get_secant_slope_group(x = k.get_value(), graph = graph2, dx = 0.01, secant_line_length=5)\n",
    "        )\n",
    "        dot2 = always_redraw(\n",
    "            lambda: Dot().move_to(plane.coords_to_point(\n",
    "                k.get_value(), graph2.underlying_function(k.get_value()))\n",
    "            )\n",
    "        )\n",
    "\n",
    "        graph2_c = graph2.copy()\n",
    "        self.play(\n",
    "            LaggedStart(DrawBorderThenFill(plane), Create(graph1), Create(graph2_c)),\n",
    "            run_time = 5,\n",
    "            lag_ratio=1,\n",
    "            \n",
    "        )\n",
    "        self.remove(graph2_c)\n",
    "        \n",
    "        self.add(slope1, slope2, dot1, dot2, graph1_lab, graph2_lab, graph2) \n",
    "        self.play(\n",
    "            k.animate.set_value(0), c.animate.set_value(2), run_time=5, rate_func = linear \n",
    "        )\n",
    "        self.play(\n",
    "            k.animate.set_value(3),\n",
    "            c.animate.set_value(-2),\n",
    "            run_time=5, \n",
    "            rate_func=linear,\n",
    "        )\n",
    "        self.wait()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "18cfc43c-0b65-43f4-bc2a-8b0a5d940995",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                               "
     ]
    },
    {
     "data": {
      "text/html": [
       "<video src=\"media\\jupyter\\CalculusArea@2024-12-08@19-42-44.mp4\" controls autoplay loop style=\"max-width: 75%;\"  >\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%manim -qm CalculusArea \n",
    "\n",
    "class CalculusArea(Scene):\n",
    "    def construct(self):\n",
    "        axes = Axes(\n",
    "            x_range=[-5,5], x_length=8, y_range=[-10,10], y_length=7\n",
    "        ).add_coordinates()\n",
    "        graph = axes.plot(lambda x:.1*(x-4)*(x-1)*(x+3), x_range=[-5,5], color=YELLOW)\n",
    "        self.add(axes, graph)\n",
    "\n",
    "        dx_list = [1, .5, .3, .1, .05, .025, .01]\n",
    "        rectangles = VGroup( \n",
    "            *[\n",
    "                axes.get_riemann_rectangles(graph=graph, x_range=[-5,5], stroke_width=.1, stroke_color=WHITE, dx=dx)\n",
    "                for dx in dx_list\n",
    "            ]\n",
    "        )\n",
    "        first_area = rectangles[0]\n",
    "        for k in range(1, len(dx_list)):\n",
    "            new_area = rectangles[k]\n",
    "            self.play(Transform(first_area, new_area), run_time=3)\n",
    "            self.wait(.5)\n",
    "        self.wait()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bb8fcf3-e3bd-4fc8-bb5a-7319513d93b8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
